package com.sx.micowell.module.course

import android.annotation.SuppressLint
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.bar.TitleBar
import com.example.mvvm.base.ContainerActivity
import com.example.mvvm.base.action.TitleBarAction
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.LogUtils
import com.gyf.immersionbar.ImmersionBar
import com.miloyu.modelnet.response.CourseVideoVO
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragCourseTrainEndBinding
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.CourseTrainEndModel
import com.sx.micowell.framework.tools.DateUtils

/**
 * @Author: YY
 * @Time: 2021/9/14
 * @Describe 课程训练播放结束
 */
@Route(path = RoutePath.COURSE_TRAIN_END)
class CourseTrainEndFragment: MyFragment<FragCourseTrainEndBinding, ContainerActivity>(), TitleBarAction {

    @Autowired(name = IntentKey.KEY)
    @JvmField
    var trainTime: Long = 0

    @Autowired(name = IntentKey.DATA)
    @JvmField
    var courseVideoData: CourseVideoVO? = null

    lateinit var viewModel: CourseTrainEndModel

    override fun initViewModel() {
        super.initViewModel()
        viewModel = getFragmentScopeViewModel(CourseTrainEndModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.frag_course_train_end, BR.viewModel, viewModel)
    }

    override fun initView() {
        // 设置状态栏字体颜色
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()
        binding.title.setOnTitleBarListener(this)
    }

    @SuppressLint("SetTextI18n")
    override fun initData() {
        LogUtils.d("initView trainTime  $trainTime")
        binding.tvTime.text = DateUtils.secToTime(trainTime)
        trainTime = if (trainTime / 1000 / 60 < 1) 1 else trainTime / 1000 / 60

        if (courseVideoData != null) {
            binding.tvCalorie.text = courseVideoData!!.curriculum.calorie
            binding.tvTitle.text = "${courseVideoData!!.detail.size}个${courseVideoData!!.detail[0].title}动作"
            viewModel.reqData(courseVideoData!!.curriculum.curriculumType, courseVideoData!!.curriculum.id,
                trainTime.toInt(),  courseVideoData!!.curriculum.calorie)
        }

    }

    override fun getTitleBar(): TitleBar {
        return binding.title
    }

    override fun onLeftClick(v: View?) {
        myActivity.finish()
    }
}